package com.zretc.support.dao;

import com.zretc.support.entity.Topicsingle;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Repository
public interface TopicsingleDao extends JpaRepository<Topicsingle,String>, JpaSpecificationExecutor<Topicsingle>,
        CrudRepository<Topicsingle,String>, Serializable {

    /**
     * 根据试题编号查询
     * @param topicId
     * @return
     */
    Topicsingle findByTopicId(String topicId);

    /**
     * 精确查找
     * @param keywords
     * @return
     */
    @Query(value = "select * from Topicsingle where topicName = ?1", nativeQuery = true)
    List<Topicsingle> getAllByTopicName(String keywords);

    /**
     * 分页+根据题目模糊查找
     * @param keywords
     * @param pageable
     * @return
     */
    @Query(value = "select * from Topicsingle where topicName like %?1% and topicLevel like %?2% and topicType like %?3%",nativeQuery = true)
    Page<Topicsingle> getAllTopicsSingleByPage(String keywords,String level,String type, Pageable pageable);


    @Query(value = "select * from Topicsingle where courseId in (?1) and topicLevel like %?2% and topicType like %?3%",nativeQuery = true)
    Page<Topicsingle> getTopicsingleByCourseIdWithPage(List<Long> courseId, String level, String type, Pageable pageable);

    /**
     * 删除方法
     * @param topicId
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "delete from Topicsingle where topicId = ?1",nativeQuery = true)
    int deleteByTopicId(String topicId);

    /**
     * 根据id精确查询
     * @param topicId
     * @return
     */
    @Query(value = "select * from Topicsingle where topicId = ?1",nativeQuery = true)
    Topicsingle getByTopicId(String topicId);

    /**
     * 随机判断题
     * @param pageable
     * @return
     */
    @Query("SELECT t FROM Topicsingle t where t.topicType=1 and t.course.courseId = ?1 ORDER BY RAND()")
    Page<Topicsingle> getRandomJudege(long courseId,Pageable pageable);

    /**
     * 随机填空题
     * @param pageable
     * @return
     */
    @Query("SELECT t FROM Topicsingle t where t.topicType=2 and t.course.courseId = ?1 ORDER BY RAND()")
    Page<Topicsingle> getRandomInsert(long courseId,Pageable pageable);

    /**
     * 随机综合题
     * @param pageable
     * @return
     */
    @Query("SELECT t FROM Topicsingle t where t.topicType=3 and t.course.courseId = ?1 ORDER BY RAND()")
    Page<Topicsingle> getRandomObject(long courseId,Pageable pageable);

    /**
     * 根据id数组查询试题
     * @param topicId
     * @return
     */
    @Query(value = "select * from Topicsingle where topicId in (?1)",nativeQuery = true)
    List<Topicsingle> getByTopicInId(List<String> topicId);

    Page<Topicsingle> getAllByTopicId(String TopicId,Pageable pageable);
}
